const state = {
  tags: [],
};
const getters = {
  tags: state => state.tags,
};
const mutations = {
  addTag(state, route) {
    if (state.tags.some(item => item.path === route.path)) return;
    state.tags.push(Object.assign({}, route));
  },
  delTag(state, path) {
    state.tags.forEach((item, index) => {
      if (item.path === path) {
        state.tags.splice(index, 1);
      }
    });
  },
  delOthersTags(state, route) {
    state.tags = state.tags.filter(item => {
      return item.meta.affix || item.path === route.path;
    });
  },
  delLeftTags(state, route) {
    let index = state.tags.length;
    state.tags = state.tags.filter(item => {
      if (item.name === route.name) {
        index = state.tags.indexOf(item);
      }
      return item.meta.affix || index <= state.tags.indexOf(item);
    });
  },
  delRightTags(state, route) {
    let index = state.tags.length;
    state.tags = state.tags.filter(item => {
      if (item.name === route.name) {
        index = state.tags.indexOf(item);
      }
      return item.meta.affix || index >= state.tags.indexOf(item);
    });
  },
  delAllTags(state) {
    state.tags = state.tags.filter(item => item.meta.affix);
  },
  updateTags(state, route) {
    state.tags.forEach(item => {
      if (item.path === route.path) {
        item = Object.assign(item, route);
      }
    });
  },
};
const actions = {
  async addTag({ commit }, route) {
    await commit('addTag', route);
  },
  async delTag({ commit, state }, route) {
    await commit('delTag', route);
    return [...state.tags];
  },
  async delOthersTags({ commit, state }, route) {
    await commit('delOthersTags', route);
    return [...state.tags];
  },
  async delLeftTags({ commit, state }, route) {
    await commit('delLeftTags', route);
    return [...state.tags];
  },
  async delRightTags({ commit, state }, route) {
    await commit('delRightTags', route);
    return [...state.tags];
  },
  async delAllTags({ commit, state }, route) {
    await commit('delAllTags', route);
    return [...state.tags];
  },

  async updateTag({ commit, state }, route) {
    await commit('updateTag', route);
    return [...state.tags];
  },
};
export default { state, getters, mutations, actions };
